package cn.iocoder.yudao.module.vote.controller.admin.voterecorddetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vote.controller.admin.voterecorddetail.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voterecorddetail.VoteRecordDetailDO;
import cn.iocoder.yudao.module.vote.convert.voterecorddetail.VoteRecordDetailConvert;
import cn.iocoder.yudao.module.vote.service.voterecorddetail.VoteRecordDetailService;

@Api(tags = "管理后台 - 投票记录详情")
@RestController
@RequestMapping("/vote/record-detail")
@Validated
public class VoteRecordDetailController {

    @Resource
    private VoteRecordDetailService recordDetailService;

    @PostMapping("/create")
    @ApiOperation("创建投票记录详情")
    @PreAuthorize("@ss.hasPermission('vote:record:create')")
    public CommonResult<Integer> createRecordDetail(@Valid @RequestBody VoteRecordDetailCreateReqVO createReqVO) {
        return success(recordDetailService.createRecordDetail(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新投票记录详情")
    @PreAuthorize("@ss.hasPermission('vote:record:update')")
    public CommonResult<Boolean> updateRecordDetail(@Valid @RequestBody VoteRecordDetailUpdateReqVO updateReqVO) {
        recordDetailService.updateRecordDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除投票记录详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('vote:record:delete')")
    public CommonResult<Boolean> deleteRecordDetail(@RequestParam("id") Integer id) {
        recordDetailService.deleteRecordDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得投票记录详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('vote:record:query')")
    public CommonResult<VoteRecordDetailRespVO> getRecordDetail(@RequestParam("id") Integer id) {
        VoteRecordDetailDO recordDetail = recordDetailService.getRecordDetail(id);
        return success(VoteRecordDetailConvert.INSTANCE.convert(recordDetail));
    }

    @GetMapping("/list")
    @ApiOperation("获得投票记录详情列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('vote:record:query')")
    public CommonResult<List<VoteRecordDetailRespVO>> getRecordDetailList(@RequestParam("ids") Collection<Integer> ids) {
        List<VoteRecordDetailDO> list = recordDetailService.getRecordDetailList(ids);
        return success(VoteRecordDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得投票记录详情分页")
    @PreAuthorize("@ss.hasPermission('vote:record:query')")
    public CommonResult<PageResult<VoteRecordDetailRespVO>> getRecordDetailPage(@Valid VoteRecordDetailPageReqVO pageVO) {
        PageResult<VoteRecordDetailDO> pageResult = recordDetailService.getRecordDetailPage(pageVO);
        return success(VoteRecordDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出投票记录详情 Excel")
    @PreAuthorize("@ss.hasPermission('vote:record:export')")
    @OperateLog(type = EXPORT)
    public void exportRecordDetailExcel(@Valid VoteRecordDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VoteRecordDetailDO> list = recordDetailService.getRecordDetailList(exportReqVO);
        // 导出 Excel
        List<VoteRecordDetailExcelVO> datas = VoteRecordDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "投票记录详情.xls", "数据", VoteRecordDetailExcelVO.class, datas);
    }

}
